/**
 * 
 */
package com.zufangbao.earth.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.InfoMessage;
import com.demo2do.core.web.resolver.Preloading;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.entity.payment.config.PaymentInstitutionParam;
import com.zufangbao.sun.service.AppService;
import com.zufangbao.sun.service.PaymentInstitutionService;

/**
 * @author lute
 *
 */

@Controller
@RequestMapping("/apps/{appId}/payment-params")
public class AppPaymentParamController {
	
	@Autowired
	private AppService appService;
	
	@Autowired
	private PaymentInstitutionService paymentInstitutionService;
	
	/**
	 * Prepare to edit app payment param
	 * 
	 * @param appId
	 * @return
	 */
	@RequestMapping("/{paymentParamId}/edit")
	public ModelAndView editPaymentParam(@PathVariable("appId") Long appId, @PathVariable("paymentParamId") Long paymentParamId) {
		
		ModelAndView modelAndView = new ModelAndView("app/payment/payment-param-edit");		
		
		App app = appService.load(App.class, appId);
		PaymentInstitutionParam appPaymentParam = paymentInstitutionService.loadPaymentInstitutionParam(paymentParamId);
		
		modelAndView.addObject("appPaymentParam", appPaymentParam);
		modelAndView.addObject("app", app);
		
		return modelAndView;
	}
	
	/**
	 * Edit app payment param
	 * 
	 * @param appId
	 * @return
	 */
	@RequestMapping(value = "/{paymentParamId}", method = RequestMethod.PATCH)
	@InfoMessage("info.app-payment-param.edit.success")
	public String onEditPaymentParam(@PathVariable("appId") Long appId,@Preloading PaymentInstitutionParam paymentInstitutionParam) {
		
		paymentInstitutionService.updatePaymentInstitutionParam(paymentInstitutionParam);

		return "redirect:/apps/" + appId + "/config?channel=" + paymentInstitutionParam.getChannel().getDescription();
	}

}
